﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Lenovo.CFI.BLL;
using Lenovo.CFI.Common.Dic;
using Lenovo.CFI.WebControls;
using Lenovo.CFI.Web.Helper;
using Lenovo.CFI.Common;
using System.Web.UI.HtmlControls;
using Lenovo.CFI.BLL.Sys;

namespace Lenovo.CFI.Web.VP
{
    public partial class CfgParas : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                TopicBl topicBl = new TopicBl();
                this.UacReviewers.Value = topicBl.GetDefaultReviewer();
                ScriptManager.RegisterStartupScript(this, this.GetType(), this.UacReviewers.ClientID,
                    this.UacReviewers.GetJsInitFunction(false) + ";", true);

                this.RepDcAndIp.DataSource = topicBl.GetDefaultDCOwner();
                this.RepDcAndIp.DataBind();
                this.OuacIpOwner.Text = topicBl.GetDefaultIPOwner();

                MessageHelper.Prepare(this.Page);
            }
        }

        protected void RepDcAndIp_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                IdeaType ideaType = (IdeaType)e.Item.DataItem;
                ((Label)e.Item.FindControl("LblDcOwner")).Text = ideaType.Title;
                ((HtmlInputHidden)e.Item.FindControl("HiDcType")).Value = ideaType.Code;
                ((OneUserAutoComplete)e.Item.FindControl("OuacDcOwner")).Text = ideaType.DC;
            }
        }

        protected void BtnSaveReviewer_Click(object sender, EventArgs e)
        {
            try
            {
                new TopicBl().SaveDefaultReviewer(this.UacReviewers.Value);

                MessageHelper.RegShowJSAjax(this.BtnSaveReviewer, "Success!", this.Page);

                ScriptManager.RegisterStartupScript(this.BtnSaveReviewer, this.BtnSaveReviewer.GetType(), this.UacReviewers.ClientID,
                    this.UacReviewers.GetJsInitFunction(false) + ";", true);
            }
            catch (ApplicationException ex)
            {
                MessageHelper.RegShowJSAjax(this.BtnSaveReviewer, ex.Message, this.Page);
            }
            catch (Exception ex)            // Ajax无法使用默认异常处理程序处理异常
            {
                ErrorHelper.ExceptionHanderForAjax(ex, this.BtnSaveReviewer, this.Page);
            }
        }

        protected void BtnSaveDcIp_Click(object sender, EventArgs e)
        {
            Dictionary<string, string> dcUsers = new Dictionary<string, string>();
            foreach (RepeaterItem row in RepDcAndIp.Items)
            {
                if (row.ItemType == ListItemType.Item || row.ItemType == ListItemType.AlternatingItem)
                {
                    string code = (((HtmlInputHidden)row.FindControl("HiDcType"))).Value;
                    dcUsers.Add(code, ((OneUserAutoComplete)row.FindControl("OuacDcOwner")).Text);
                }
            }
            string ipUser = this.OuacIpOwner.Text;

            try
            {
                TopicBl topicBl = new TopicBl();
                topicBl.SaveDefaultDCOwner(dcUsers);
                topicBl.SaveDefaultIPOwner(ipUser);

                MessageHelper.RegShowJSAjax(this.BtnSaveDcIp, "Success!", this.Page);
            }
            catch (ApplicationException ex)
            {
                MessageHelper.RegShowJSAjax(this.BtnSaveDcIp, ex.Message, this.Page);
            }
            catch (Exception ex)            // Ajax无法使用默认异常处理程序处理异常
            {
                ErrorHelper.ExceptionHanderForAjax(ex, this.BtnSaveDcIp, this.Page);
            }
        }

        protected void BtnSaveCfiFile_Click(object sender, EventArgs e)
        {
            try
            {
                Guid guid = FileDUHelper.UploadAttach(this.FuCfiFile);

                new SysParaBl().SaveTemplateCfi(guid);

                MessageHelper.Show("Success!", this.Page);
            }
            catch (ApplicationException ex)
            {
                MessageHelper.Show(ex.Message, this.Page);
            }
            catch (Exception ex) 
            {
                ErrorHelper.Handle(ex);
            }
        }

        protected void BtnSaveCsFile_Click(object sender, EventArgs e)
        {
            try
            {
                Guid guid = FileDUHelper.UploadAttach(this.FuCsFile);

                new SysParaBl().SaveTemplateCSuggestion(guid);

                MessageHelper.Show("Success!", this.Page);
            }
            catch (ApplicationException ex)
            {
                MessageHelper.Show(ex.Message, this.Page);
            }
            catch (Exception ex)
            {
                ErrorHelper.Handle(ex);
            }
        }
    }
}